package com.ruoyi.znjc.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PestDisease2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.znjc.mapper.ZnjcRuleswarnlogMapper;
import com.ruoyi.znjc.domain.ZnjcRuleswarnlog;
import com.ruoyi.znjc.service.IZnjcRuleswarnlogService;

/**
 * 规则预警日志Service业务层处理
 * 
 * @author ZNJC
 * @date 2025-07-23
 */
@Service
public class ZnjcRuleswarnlogServiceImpl implements IZnjcRuleswarnlogService 
{

    @Autowired
    private ZnjcRuleswarnlogMapper znjcRuleswarnlogMapper;

    // 其它已有方法 ...

    @Override
    public int processAndInsertLogByTimerId(Long timerId, String ruleSnapshot) {
        // 1. 根据 timerId 查 usersubId
        Long usersubId = znjcRuleswarnlogMapper.selectUsersubIdByTimerId(timerId);
        if (usersubId == null) {
            throw new RuntimeException("无法根据 timerId 查到对应 usersubId");
        }

        // 2. 根据 usersubId 查 ruleId
        Long ruleId = znjcRuleswarnlogMapper.selectRuleIdByUsersubId(usersubId);
        if (ruleId == null) {
            throw new RuntimeException("无法根据 usersubId 查到对应 ruleId");
        }

        // 3. 根据 ruleId 查 rule_content
        String ruleContent = znjcRuleswarnlogMapper.selectRuleContentByRuleId(ruleId);
        if (ruleContent == null) {
            throw new RuntimeException("无法根据 ruleId 查到对应 ruleContent");
        }

        // 4. 组装实体并插入日志
        ZnjcRuleswarnlog log = new ZnjcRuleswarnlog();
        log.setTimerId(timerId);
        log.setSubId(usersubId);
        log.setRuleId(ruleId);
        log.setRuleSnapshot(ruleContent);   // 或传入的 ruleSnapshot，根据实际
        log.setCreateTime(DateUtils.getNowDate());

        return znjcRuleswarnlogMapper.insertRuleswarnlogDirect(log);
    }



    @Override
    public List<Map<String, Object>> selectPestDiseaseWithCategoryName(PestDisease2 pestDisease) {
        return znjcRuleswarnlogMapper.selectPestDiseaseWithCategoryName(pestDisease);
    }


    /**
     * 查询规则预警日志
     * 
     * @param logId 规则预警日志主键
     * @return 规则预警日志
     */
    @Override
    public ZnjcRuleswarnlog selectZnjcRuleswarnlogByLogId(Long logId)
    {
        return znjcRuleswarnlogMapper.selectZnjcRuleswarnlogByLogId(logId);
    }

    /**
     * 查询规则预警日志列表
     * 
     * @param znjcRuleswarnlog 规则预警日志
     * @return 规则预警日志
     */
    @Override
    public List<ZnjcRuleswarnlog> selectZnjcRuleswarnlogList(ZnjcRuleswarnlog znjcRuleswarnlog)
    {
        return znjcRuleswarnlogMapper.selectZnjcRuleswarnlogList(znjcRuleswarnlog);
    }

    /**
     * 新增规则预警日志
     * 
     * @param znjcRuleswarnlog 规则预警日志
     * @return 结果
     */
    @Override
    public int insertZnjcRuleswarnlog(ZnjcRuleswarnlog znjcRuleswarnlog)
    {
        znjcRuleswarnlog.setCreateTime(DateUtils.getNowDate());
        return znjcRuleswarnlogMapper.insertZnjcRuleswarnlog(znjcRuleswarnlog);
    }

    /**
     * 修改规则预警日志
     * 
     * @param znjcRuleswarnlog 规则预警日志
     * @return 结果
     */
    @Override
    public int updateZnjcRuleswarnlog(ZnjcRuleswarnlog znjcRuleswarnlog)
    {
        return znjcRuleswarnlogMapper.updateZnjcRuleswarnlog(znjcRuleswarnlog);
    }

    /**
     * 批量删除规则预警日志
     * 
     * @param logIds 需要删除的规则预警日志主键
     * @return 结果
     */
    @Override
    public int deleteZnjcRuleswarnlogByLogIds(Long[] logIds)
    {
        return znjcRuleswarnlogMapper.deleteZnjcRuleswarnlogByLogIds(logIds);
    }

    /**
     * 删除规则预警日志信息
     * 
     * @param logId 规则预警日志主键
     * @return 结果
     */
    @Override
    public int deleteZnjcRuleswarnlogByLogId(Long logId)
    {
        return znjcRuleswarnlogMapper.deleteZnjcRuleswarnlogByLogId(logId);
    }




}
